//
//  XTWriteDairyViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-5-19.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTWriteDairyViewController.h"
#import "XTDairyViewController.h"
#import "XTMyDairy.h"
@interface XTWriteDairyViewController ()<UIAlertViewDelegate>
@property(nonatomic, retain)UITextField *titleName;
@property(nonatomic, retain)UITextField *zhengWen;
@property(nonatomic, retain)XTMyDairy *xtMyDairy;
@property(nonatomic, retain)UIAlertView *alertView;
@end

@implementation XTWriteDairyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"写日记";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(faBiao)];
    self.imageViewBase = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imageViewBase.image = [UIImage imageNamed:@"999b"];
    [self.view addSubview:_imageViewBase];
    //标题
    self.titleName = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    _titleName.backgroundColor = [UIColor lightGrayColor];
    _titleName.placeholder = @"请输入标题";
    _titleName.font = [UIFont systemFontOfSize:20];
    _titleName.clearsOnBeginEditing = YES;
    [self.view addSubview:_titleName];
    [_titleName release];
    
    
    //正文
    self.zhengWen = [[UITextField alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, 100)];
    _zhengWen.backgroundColor = [UIColor lightGrayColor];
    _zhengWen.placeholder = @"添加正文";
    _zhengWen.clearsOnBeginEditing = YES;
    [self.view addSubview:_zhengWen];
    [_zhengWen release];
    
    
}
- (void)faBiao
{
    self.xtMyDairy = [[XTMyDairy alloc] init];
    _xtMyDairy.myDairyTitle = self.titleName.text;
    _xtMyDairy.myDairyContent = self.zhengWen.text;
    if ([self.titleName.text isEqualToString:@""]) {
        self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"还没写任何内容呦~~" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [self.alertView show];
    } else {
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        //创建文件管理器(单例)
        NSFileManager *fileManager = [NSFileManager defaultManager];
        //添加文件夹
        //拼接文件夹地址
        NSString *filePath = [path stringByAppendingPathComponent:@"收藏"];
        //创建文件夹
        [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        NSString *strPath = [filePath stringByAppendingPathComponent:@"myDairy.ddp"];
        if (![fileManager fileExistsAtPath:strPath]) {
            NSMutableArray *arr = [NSMutableArray array];
            [arr addObject:self.xtMyDairy];
            [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
            self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存成功" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [_alertView show];
        } else {
            NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
            NSMutableArray *newArr = [NSMutableArray array];
            for (XTMyDairy *xtMD in arr) {
                [newArr addObject: xtMD.myDairyTitle];
            }
            if (![newArr containsObject:self.xtMyDairy.myDairyTitle]) {
                [arr addObject:self.xtMyDairy];
                [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
                self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存成功" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [_alertView show];
            } else {
                self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已保存过同名日记,请更改名字" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
                [_alertView show];
            }
        }

    }
    
}
- (void)dismissAlertView
{
    [_alertView dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.delegate getPrama:self.xtMyDairy];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
